package com.example.phoneshop.service.impl;

import com.example.phoneshop.dao.OrderDao;
import com.example.phoneshop.entity.Address;
import com.example.phoneshop.entity.Order;
import com.example.phoneshop.entity.OrderPage;
import com.example.phoneshop.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @author nozomi
 * @date 2018/7/8 17:03
 * @description
 */
@Service
public class OrderServiceImpl implements OrderService {
    private OrderDao orderDao;
    @Autowired
    public OrderServiceImpl(OrderDao orderDao){
        this.orderDao=orderDao;

    }
    @Override
    public void addOrder(Order order){
      orderDao.addOrder(order);
    }

    @Override
    public void addAddress(Address address){
        orderDao.addAddress(address);
    }
    @Override
    public void updateAddress(Address address){
        orderDao.updateAddress(address);
    }
    @Override
    public Address selectAddrByName(String username){
        return orderDao.selectByName(username);
    }

    @Override
    public Order selectById(int id){return orderDao.selectById(id);}

    @Override
    public void updateState(Order order){orderDao.updateState(order);}

    @Override
    public List<Order> getMyOrders(String username){
        return orderDao.getMyOrder(username);
    }
    @Override
    public OrderPage<Order> findOrderByPage(int currentPage){
            HashMap<String,Object> map=new HashMap<>();
            OrderPage<Order> pageBean=new OrderPage<>();
            int pageSize=5;
            pageBean.setPageSize(pageSize);
            int totalCount=orderDao.orderCount();
            pageBean.setTotalCount(totalCount);
            Double num=Math.ceil((double) totalCount /pageSize);
            pageBean.setTotalPage(num.intValue());
            map.put("start",(currentPage-1)*pageSize);
            map.put("size",pageBean.getPageSize());
            List<Order> lists=orderDao.getOrderList(map);
            pageBean.setLists(lists);
            return pageBean;

    }


}
